<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>子组件修改父组件数据</title>
    <script src="node_modules/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
    <h2>num = {{ num }}</h2>
    <counter @plus="numPlus" @reduce="numReduce" :snum="num"></counter>
</div>
<script type="text/javascript">
    const counter = {
        template: `
          <div>
          <button @click='incrNum'>+</button>
          <button @click='decrNum'>-</button>
          </div>
        `,
        props: ["snum"],
        methods: {
            incrNum() {
                //调用父组件的方法
                return this.$emit("plus");
            },
            decrNum() {
                //调用父组件的方法
                return this.$emit("reduce");
            }
        }
    };
    const app = new Vue({
        el: "#app",
        components: {
            counter
        },
        data: {
            num: 0
        },
        methods: {
            numPlus() {
                this.num++;
            },
            numReduce() {
                this.num--;
            }
        }
    });
</script>
</body>
</html>